
package com.wwy.monitor.util;

import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * Title: SpringContextUtil
 * Description:
 * Date: 16/6/7 ä¸å10:59
 * Company: é«å¾·ä¿¡æ¯ææ¯æéå¬å¸
 *
 * @author jl107065 jl107065@autonavi.com
 * @see
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {
    // Springåºç¨ä¸ä¸æç¯å¢
    private static ApplicationContext applicationContext;

    /**
     * å®ç°ApplicationContextAwareæ¥å£çåè°æ¹æ³ï¼è®¾ç½®ä¸ä¸æç¯å¢
     *
     * @param applicationContext
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        SpringContextUtil.applicationContext = applicationContext;
    }

    /**
     * @return ApplicationContext
     */
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    /**
     * è·åå¯¹è±¡
     *
     * @param name
     * @return Object
     * @throws BeansException
     */
    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }

    public static <T> Map<String, T> getBeans(Class<T> clazz) throws BeansException {
        return applicationContext.getBeansOfType(clazz);
    }
}